package cn.livingCloud.controller.core.validator;

import cn.livingCloud.dto.CaseCategoryFormDto;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by 唐旭 on 2016/6/2.
 */
@Component
public class CaseCategoryFormDtoValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return CaseCategoryFormDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        CaseCategoryFormDto formDto = (CaseCategoryFormDto) target;
        validationName(formDto, errors);
        validationContent(formDto, errors);
    }

    private void validationContent(CaseCategoryFormDto formDto, Errors errors) {
        if (StringUtils.isEmpty(formDto.getContent())) {
            errors.rejectValue("content", null, "分类描述不能为空");
        }
    }

    private void validationName(CaseCategoryFormDto formDto, Errors errors) {
        if (StringUtils.isEmpty(formDto.getName())) {
            errors.rejectValue("name", null, "名称不能为空");
        }
    }
}
